﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebSocketClient
{
    public sealed class PassthroughTrackingStream : Stream
    {
        private readonly Stream _stream;
        public long BytesRead;

        public PassthroughTrackingStream(Stream stream) => _stream = stream;

        public override bool CanWrite => true;
        public override bool CanRead => true;

        public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
        {
            int n = await _stream.ReadAsync(buffer, cancellationToken);
            BytesRead += n;
            return n;
        }

        public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken) =>
            _stream.WriteAsync(buffer, cancellationToken);

        protected override void Dispose(bool disposing) => _stream.Dispose();
        public override bool CanSeek => _stream.CanSeek;
        public override long Length => _stream.Length;
        public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
        public override void Flush() { _stream.Flush(); }
        public override int Read(byte[] buffer, int offset, int count) => _stream.Read(buffer, offset, count);
        public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin);
        public override void SetLength(long value) => _stream.SetLength(value);
        public override void Write(byte[] buffer, int offset, int count) => _stream.Write(buffer, offset, count);
    }
}
